//
//  JQAppServiceManager.h
//  JQSOAppDelegate
//
//  Created by JQT on 2018/1/20.
//  Copyright © 2018年 JQ. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

/**
 *  注册服务 & 定义服务名称 & 定义服务单例
 */
#define JQ_EXPORT_SERVICE(name) \
+ (void)load {[[JQAppServiceManager sharedManager] registerService:[[self class] sharedInstance]];} \
- (NSString *)serviceName { return @#name; } \
+ (instancetype)sharedInstance { \
    static id sharedInstance = nil; \
    static dispatch_once_t onceToken; \
    dispatch_once(&onceToken, ^{ \
        Class serviceClass = [self class]; \
        sharedInstance = [[serviceClass alloc] init]; \
    }); \
    return sharedInstance; \
}

NS_ASSUME_NONNULL_BEGIN

/**
 *
 *  @brief App service protocol
 *
 */
@protocol JQAppService <UIApplicationDelegate>

@required
/**
 *
 *  @brief 服务名
 *
 */
- (NSString *)serviceName;

/**
 *
 *  @brief 服务单例
 *
 */
+ (instancetype)sharedInstance;

@end

/**
 *
 *  @brief 服务管理器
 *
 */
@interface JQAppServiceManager : NSObject

/**
 *
 *  @brief 单例
 *
 */
+ (instancetype)sharedManager;

/**
 *
 *  @brief 注册一个服务
 *
 *  @param service 模块实例对象
 */
- (void)registerService:(id<JQAppService>)service;

/**
 *
 *  @brief 移除一个服务
 *
 *  @param service 模块实例对象
 */
- (void)unRegisterService:(id<JQAppService>)service;

/**
 *
 *  @brief 检查协议方法是否可以执行
 *
 *  @param aSelector 协议方法
 */
- (BOOL)proxyCanResponseToSelector:(SEL)aSelector;

/**
 *
 *  @brief 调用协议
 *
 *  @param anInvocation 调用
 */
- (void)proxyForwardInvocation:(NSInvocation *)anInvocation;

@end

NS_ASSUME_NONNULL_END
